package com.army.study.service;

import com.army.study.BusCardConfigAbility;
import com.army.study.MainAbility;
import com.army.study.database.Bus;
import com.army.study.database.Station;
import com.army.study.utils.LogUtils;
import ohos.ace.ability.AceInternalAbility;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.utils.zson.ZSONObject;

import java.util.List;

/**
 * 与前端js目录下default页面打交道的Ability
 */
public class BusInternalAbility extends AceInternalAbility {
    private static final int BUS_LEVEL_NOT_AVAILABLE = 1001;
    private static final int STATION_LEVEL_NOT_AVAILABLE = 1002;
    private static final int BINDING_CARD_NOT_AVAILABLE = 1003;

    private static BusInternalAbility instance;

    private static final String BUNDLE_NAME = "com.army.study";

    private static final String ABILITY_NAME = "com.army.study.service.BusInternalAbility";

    private static final String[] BUTTON_STATE_IMAGE = new String[]{"/common/images/checkbox-blank.png", "/common/images/checkbox-circle.png"};


    public BusInternalAbility() {
        super(BUNDLE_NAME, ABILITY_NAME);
    }

    /**
     * 单例模式生成Bus Internal Ability
     *
     * @return
     */
    public static BusInternalAbility getInstance() {
        if (instance == null) {
            synchronized (BusInternalAbility.class) {
                if (instance == null) {
                    instance = new BusInternalAbility();
                }
            }
        }
        return instance;
    }

    /**
     * 注册
     */
    public void register() {
        LogUtils.debug("BusInternalAbility", "Register");
        System.out.println("Register");
        this.setInternalAbilityHandler(this::onRemoteRequest);
    }

    /**
     * 取消注册
     */
    public void unregister() {
        LogUtils.debug("BusInternalAbility", "Unregister");
        this.setInternalAbilityHandler(null);
    }


    /**
     * 根据JS前端传过来的Code判断返回相应的数据
     * @param code
     * @param data
     * @param reply
     * @param option
     * @return
     */
    private boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
        System.out.println("onRemoteRequest........ " + code);
        if (BUS_LEVEL_NOT_AVAILABLE == code) {
            reply.writeString(replyBusResult(data));
            return true;
        }else if (STATION_LEVEL_NOT_AVAILABLE == code) {
            reply.writeString(replyStationResult(data));
            return true;
        }else if (BINDING_CARD_NOT_AVAILABLE == code) {
            reply.writeString(replyBindingCardResult(data));
            return true;
        }else {
            reply.writeString("service not defined");
            return false;
        }
    }

    /**
     * 根据搜索关键字返回公交车信息
     * @param data
     * @return
     */
    private String replyBusResult(MessageParcel data) {
        String objStr = data.readString();
        ZSONObject zsonObject = ZSONObject.stringToZSON(objStr);
        String keyword = zsonObject.getString("keyword");
        String result = getBusInfo(keyword);
        LogUtils.debug("BusInternalAbility", result);
        return result;
    }

    /**
     * 根据公交车Id返回公交车经过的站牌
     * @param data
     * @return
     */
    private String replyStationResult(MessageParcel data) {
        String objStr = data.readString();
        ZSONObject zsonObject = ZSONObject.stringToZSON(objStr);
        Long parentId = zsonObject.getLong("parentId");
        String result = getStationInfo(parentId);
        LogUtils.debug("StationInternalAbility", result);
        return result;
    }

    /**
     * 绑定卡片和公交车
     * @param data
     * @return
     */
    private String replyBindingCardResult(MessageParcel data) {
        String objStr = data.readString();
        ZSONObject zsonObject = ZSONObject.stringToZSON(objStr);
        Long busId = zsonObject.getLong("busId");

        // 绑定卡片和公交车
        Long formId = BusCardConfigAbility.formId;

        System.out.println("FormId: " + formId + ", busId: " + busId);
        MainAbility mainAbility = new MainAbility();
        mainAbility.bindingCardById(formId, busId);

        return "OK";
    }

    /**
     * 根据keyword到数据库查找公交车信息
     * 如果没有公交车信息, 初始化公交车信息
     * @param keyword
     * @return
     */
    private String getBusInfo(String keyword) {
        System.out.println("获取公交信息...");
        MainAbility mainAbility = new MainAbility();
        List<Bus> busList = mainAbility.getBusListByKeyword(keyword);

        busList.stream().forEach(bus -> {
            bus.setCheckBtn(BUTTON_STATE_IMAGE[0]);
        });

        String result = ZSONObject.toZSONString(busList);
        LogUtils.debug("BusInternalAbility", "getBusInfo: " + result);
        return result;
    }

    /**
     * 根据公交车Id到数据库查找站牌信息
     * 如果没有站牌信息,初始化公交车信息
     * @param parentId
     * @return
     */
    private String getStationInfo(Long parentId) {
        MainAbility mainAbility = new MainAbility();
        List<Station> stationList = mainAbility.getStationListByParentId(parentId);

        String result = ZSONObject.toZSONString(stationList);
        LogUtils.debug("BusInternalAbility", "getStationInfo: " + result);
        return result;
    }
}
